package com.sskj.one.mine;

import android.Manifest;
import android.arch.lifecycle.Observer;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.RxPermissions;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.one.R;
import com.sskj.one.component.DaggerUserDataComponent;
import com.sskj.one.view.SelectPictureDialog;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/10
 */
public class AddAlipayActivity extends BaseActivity<AddAlipayPresenter> {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_account_alipay)
    EditText etAccountAlipay;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.iv_qrcode)
    ImageView iv_qrcode;
    @BindView(R.id.tv_qrcode)
    TextView tv_qrcode;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.ll_pay_pwd)
    LinearLayout ll_pay_pwd;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private SelectPictureDialog selectPictureDialog;
    private String mImagePath;


    @Override
    public int getLayoutId() {
        return R.layout.app_activity_add_alipay;
    }

    @Override
    public AddAlipayPresenter getPresenter() {
        return new AddAlipayPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, new Observer<List<UserData>>() {
            @Override
            public void onChanged(@Nullable List<UserData> users) {
                if (users != null && users.size() > 0) {
                    userData = users.get(0);
                    if (isEmpty(userData.getAlipay_nums())) {
                        tvTitle.setText("添加结汇方式");
                        ImageUtil.setImage(ContextCompat.getDrawable(AddAlipayActivity.this, R.mipmap.icon_upload), iv_qrcode);
                    } else {
                        tvTitle.setText("结汇方式");
                        etName.setEnabled(false);
                        etAccountAlipay.setEnabled(false);
                        iv_qrcode.setEnabled(false);
                        ImageUtil.setImage(BaseHttpConfig.BASE_URL + userData.getAlipay_img(), iv_qrcode);
                        etName.setText(userData.getAlipay_username());
                        etAccountAlipay.setText(userData.getAlipay_nums());
                        view.setVisibility(View.GONE);
                        btnConfirm.setVisibility(View.GONE);
                        ll_pay_pwd.setVisibility(View.GONE);
                    }
                }
            }
        });
    }


    @Override
    public void initData() {
        ClickUtil.click(300, btnConfirm, () -> {
            if (userData == null) {
                ToastUtils.show("获取用户信息失败");
                return;
            }
            if (isEmpty(etName)) {
                toastHint(etName);
                return;
            }
            if (isEmpty(etAccountAlipay)) {
                toastHint(etAccountAlipay);
                return;
            }
            if (isEmpty(etPayPwd)) {
                toastHint(etPayPwd);
                return;
            }
            if (isEmpty(mImagePath)) {
                ToastUtils.show("请先上传支付宝二维码");
                return;
            }
            mPresenter.addAlipay(getText(etName), getText(etAccountAlipay), getText(etPayPwd), new File(mImagePath));
        });
        ClickUtil.click(300, iv_qrcode, () -> {
            new RxPermissions(AddAlipayActivity.this)
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                    .subscribe(granted -> {
                        if (granted) {
                            selectPic();
                        }
                    });
        });
    }

    private void selectPic() {
        if (selectPictureDialog == null) {
            selectPictureDialog = new SelectPictureDialog(this);
        }
        selectPictureDialog.setOnSelectListener(new SelectPictureDialog.SelectListener() {
            @Override
            public void selectByAlbum() {
                PictureSelector.create(AddAlipayActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }

            @Override
            public void selectByPhoto() {
                PictureSelector.create(AddAlipayActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .setOutputCameraPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }
        });
        selectPictureDialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片、视频、音频选择结果回调
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            mImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
            ImageUtil.setImage(mImagePath, iv_qrcode);
        }
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AddAlipayActivity.class);
        context.startActivity(intent);
    }

    public void addAlipayOK(String name, String account) {
        userData.setAlipay_nums(account);
        userData.setAlipay_username(name);
        userViewModel.update(userData);
        finish();
    }
}
